package com.atguigu.gmall.order.config.mq;

import com.atguigu.gmall.common.consts.RabbitConst;
import net.sf.jsqlparser.statement.select.Top;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @Author:lyd
 * 创建order服务需要的交换机和队列
 **/
@Configuration
public class RabbitMqConfig {

    /**
     * 创建订单服务的交换机
     * String name,
     * boolean durable,
     * boolean autoDelete,
     * Map<String, Object> arguments
     * @return
     */
    @Bean
    public Exchange orderEventExchange() {
        TopicExchange exchange = new TopicExchange(
                RabbitConst.EXCHANGE_ORDER_EVENT,
                true,
                false
        );
        return exchange;
    }

    /**
     * 创建订单服务的延时队列
     * @return
     */
    @Bean
    public Queue orderCreateQueue(){
        HashMap<String, Object> map = new HashMap<>();
        //设置队列过期时间
        map.put("x-message-ttl",30*60000);//订单30分钟不支付关闭
        map.put("x-dead-letter-exchange",RabbitConst.EXCHANGE_ORDER_EVENT);
        map.put("x-dead-letter-routing-key",RabbitConst.ROUTING_KEY_ORDER_TIME_OUT);
        Queue queue = new Queue(
                RabbitConst.QUEUE_ORDER_CREATE,
                true,
                false,
                false,
                map
        );
        return queue;
    }

    /**
     * 创建订单队列的绑定关系
     * String destination,
     * DestinationType destinationType,
     * String exchange,
     * String routingKey,
     *@Nullable Map<String, Object> arguments
     * @return
     */
    @Bean
    public Binding orderCreateBinding() {
      return new Binding(
                RabbitConst.QUEUE_ORDER_CREATE,
                Binding.DestinationType.QUEUE,
                RabbitConst.EXCHANGE_ORDER_EVENT,
                RabbitConst.ROUTING_KEY_ORDER_CREATE,
                null
        );
    }

    /**
     * 创建死信队列
     * @return
     */
    @Bean
    public Queue orderDeadQueue() {
        return new Queue(
                RabbitConst.QUEUE_ORDER_DEAD,
                true,
                false,
                false,
                null
        );
    }

    /**
     * 创建死信队列的绑定关系
     * @return
     */
    @Bean
    public Binding orderDeadBinding() {
        return new Binding(
                RabbitConst.QUEUE_ORDER_DEAD,
                Binding.DestinationType.QUEUE,
                RabbitConst.EXCHANGE_ORDER_EVENT,
                RabbitConst.ROUTING_KEY_ORDER_TIME_OUT,
                null
        );
    }

    /**
     * 创建订单支付成功的队列
     * @return
     */
    @Bean
    public Queue orderPayedQueue() {
        return new Queue(
                RabbitConst.QUEUE_ORDER_PAYED,
                true,
                false,
                false,
                null
        );
    }

    /**
     * 创建订单支付成功的队列的绑定关系
     * @return
     */
    @Bean
    public Binding orderPayedBinding() {
        return new Binding(
                RabbitConst.QUEUE_ORDER_PAYED,
                Binding.DestinationType.QUEUE,
                RabbitConst.EXCHANGE_ORDER_EVENT,
                RabbitConst.ROUTING_KEY_ORDER_PAYED,
                null
        );
    }
}
